<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IPhoneAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/interfaces/IConnectionAccessDao.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/ConnectionFactory.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];

  	class  MySqlPhoneAccessDao implements IPhoneAccessDao {
  
		/**
		 * Constructor of the phone dao
		 */
	 	public function __construct () {}
	 
  		/**
		 * Creates a phone
		 */
		public function createPhone($phone) {
			$type = $phone -> getType();
			$serial = $phone -> getSerial();
			$number = $phone -> getNumber();
			$idOwner = $phone -> getIdOwner();
			$typeOwner = $phone -> getTypeOwner();
			
			$GLOBALS['debug'] -> log($typeOwner, "type owner");
			
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			switch ($typeOwner) {
				case 'NATURAL_CUSTOMER':
					$query = "INSERT INTO PHONE (type, serial, number, 
						fk_id_number_natural_customer) VALUES ('$type', $serial, $number, 
						$idOwner)";
				break;
				
				case 'LEGAL_CLIENT':
					$query = "INSERT INTO PHONE (type, serial, number, 
						fk_rif_legal_client) VALUES ('$type', $serial, $number, 
						'$idOwner')";
				break;
				
				default: //case 'EMPLOYEE'
					$query = "INSERT INTO PHONE (type, serial, number, 
						fk_id_number_employee) VALUES ('$type', $serial, $number, 
						$idOwner)";
				break;
			}
			
			$GLOBALS['debug'] -> log($query, "Phone query");
			
			$sentence = mysql_query($query, $conn);
			
			if ($sentence)
				return true;
			
			return false;	
		}
		
		/**
		 * Select phones by natural customer id number
		 */
		public function selectPhonesByNaturalCustomerIdNumber($ci) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT CONCAT(type , ': 0', serial, '.', number) phone
					  FROM PHONE
					  WHERE fk_id_number_natural_customer = $ci";
					  
			$sentence = mysql_query($query, $conn);
			
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		}
		
		/**
		 * Select phones by legal client rif
		 */
		public function selectPhonesByLegalClientRif($rif) {
			$connection = ConnectionFactory :: getMySqlConnectionAccessDao();
			$conn = $connection -> openConnection();
			
			$query = "SELECT CONCAT(type , ': 0', serial, '.', number) phone
					  FROM PHONE
					  WHERE fk_rif_legal_client = '$rif'";
					  
			$sentence = mysql_query($query, $conn);
			
			$conn = $connection -> closeConnection($conn);
			
			return $sentence;
		}
  	}
?>